"use strict";
module.exports = (options) => {
  return async function (ctx, next) {
    try {
      const token = ctx.request.header.authorization;
      let decode = "";
      // 解码token
      decode = ctx.app.jwt.verify(token.split(" ")[1], options.secret); //验证token
      console.log("decode======>", decode);
      // 获取用户信息
      ctx.decode = decode;
      //切记先解析token并存储数据后再执行回调，否则解析数据获取不到x
      await next();
    } catch (error) {
      ctx.status = 401;
      ctx.body = {
        code: 7001,
        data: null,
        message: error.message,
      };
    }
  };
};
